package com.wbl.train.business.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.wbl.train.business.service.DailyTrainTicketService;
import com.wbl.train.common.business.dto.DailyTrainTicketDto;
import com.wbl.train.common.business.entity.DailyTrainTicketEntity;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王博纶
 * @since 2024-02-15
 * @description 余票信息
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/admin/daily-train-ticket")
@Tag(name = "余票信息控制器类",
        description = "DailyTrainTicketController 余票信息 后端数据接口")
public class DailyTrainTicketAdminController {
    private final DailyTrainTicketService dailyTrainTicketService;

    /**
     * 通过id查询 余票信息
     * @param dailyTrainTicketDto dailyTrainTicketDto
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query")
    public R<PageVo<DailyTrainTicketEntity>> getById(DailyTrainTicketDto dailyTrainTicketDto) {
        // 火车车次查询
        log.debug("dailyTrainTicketDto :{}",dailyTrainTicketDto);
        PageVo<DailyTrainTicketEntity> pageVo = dailyTrainTicketService.queryTrainTicket(dailyTrainTicketDto);
        return R.ok("查询成功",pageVo);
    }

    /**
     * 新增 余票信息
     * @param dailyTrainTicket 余票信息
     * @return Result
     */
    @Operation(summary = "新增余票信息", description = "新增余票信息")
    @PostMapping("/save")
    public R<Boolean> save(@Valid @RequestBody DailyTrainTicketEntity dailyTrainTicket) {
        return R.ok("保存成功",dailyTrainTicketService.insertAndUpdateTrain(dailyTrainTicket));
    }

    /**
     * 通过id删除 余票信息
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除余票信息",
               description = "通过id删除余票信息")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        if(ObjectUtil.isNull(dailyTrainTicketService.getById(id))){
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前余票信息不存在");
        }
        return R.ok(dailyTrainTicketService.removeById(id));
    }

}
